using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using Entities;
using System.Configuration;
namespace DAL
{
    public class tbLoaiSPDAL
    {
        private SqlConnection cn = null;
        public tbLoaiSPDAL()
        {
            cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);           
            cn.Open();
        }
        public void Insert(tbLoaiSP lsp)
        {
            SqlCommand cm = new SqlCommand("LoaiSP_Insert", cn);
            cm.CommandType = System.Data.CommandType.StoredProcedure;
            cm.CommandType = System.Data.CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@TenLoaiSP ", lsp.TenLoaiSP);
            cm.ExecuteNonQuery();

        }
        public void Update(tbLoaiSP lsp)
        {
            SqlCommand cm = new SqlCommand("LoaiSP_Update", cn);
            cm.CommandType = System.Data.CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@TenLoaiSP ", lsp.TenLoaiSP);
            cm.ExecuteNonQuery();
        }
        public void Delete(int Id)
        {
            SqlCommand cm = new SqlCommand("LoaiSP_Delete", cn);
            cm.CommandType = System.Data.CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@IDLoaiSP ", Id);
            cm.ExecuteNonQuery();
        }
        public Entities.tbLoaiSP.tbLoaiSPCollection Get(string Where, int pageIndex, int pageSize, string orderBy, string orderDirection, out int totalRecords)
        {
            SqlCommand cm = new SqlCommand("tbLoaiSP_Get", cn);
            cm.CommandType = System.Data.CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@Where", Where);
            cm.Parameters.AddWithValue("@PageIndex", pageIndex);
            cm.Parameters.AddWithValue("@PageSize", pageSize);
            cm.Parameters.AddWithValue("@OrderBy", orderBy);
            cm.Parameters.AddWithValue("@OrderDirection", orderDirection);
            cm.Parameters.Add(new SqlParameter("@TotalRecords", System.Data.DbType.Int32)).Direction = System.Data.ParameterDirection.Output;
            SqlDataReader dr = cm.ExecuteReader();
            tbLoaiSP.tbLoaiSPCollection lspcollection = new tbLoaiSP.tbLoaiSPCollection();
            while (dr.Read())
            {
                tbLoaiSP lsp = new tbLoaiSP();
                lsp.IDLoaiSP = int.Parse (dr.GetValue(0).ToString());
                lsp.TenLoaiSP = dr.GetValue(1).ToString();
                lspcollection.Add(lsp);              
            }
            dr.Close();
            totalRecords = int.Parse(cm.Parameters["@TotalRecords"].Value.ToString());
            return lspcollection;
        }
    }
}
